//
//  HSTopicListVM.m
//  HolyShit
//
//  Created by Robin on 2017/4/4.
//  Copyright © 2017年 HolyShit. All rights reserved.
//

#import "HSTopicListVM.h"

@interface HSTopicListVM ()

@property (nonatomic, copy) NSString *slug;
@property (nonatomic, assign) NSUInteger offset;

@end

@implementation HSTopicListVM

- (instancetype)initWithSlug:(NSString *)slug
{
    if (self = [super init]) {
        _slug = slug;
    }
    return self;
}

- (RACCommand *)dataCommand{
    if(!_dataCommand){
        @weakify(self);
        NSString *url = [NSString stringWithFormat:@"%@%@?filter[q]=tag:%@",AppHost, DiscussPath,self.slug];
        _dataCommand = [RACCommand commandWithSignalBlock:^RACSignal *(id input) {
            return [[MYHttpHelper shareHelper] rac_getWithUrl:url  params:nil success:^(NSDictionary *dict) {
                if (![dict containsObjectForKey:@"data"]) {
                    return ;
                }
                @strongify(self);
                self.offset = 0;
                NSArray *array = [NSArray modelArrayWithClass:[HSForumListModel class] json:dict[@"data"]];
                [self renewForums:array withUsers:[NSArray modelArrayWithClass:[HSForumListModel class] json:dict[@"included"]]];
                self.forums = array.mutableCopy;
            } failed:^(NSError *error) {
                
            }].racSignal;
        }];
    }
    return _dataCommand;
}

- (RACCommand *)loadmoreCommand{
    if(!_loadmoreCommand){
        @weakify(self);
        NSString *url = [NSString stringWithFormat:@"%@%@?filter[q]=tag:%@&&page[offset]=%lu",AppHost, DiscussPath,self.slug,self.offset + 20];
        _loadmoreCommand = [RACCommand commandWithSignalBlock:^RACSignal *(id input) {
            return [[MYHttpHelper shareHelper] rac_getWithUrl:url  params:nil success:^(NSDictionary *dict) {
                if (![dict containsObjectForKey:@"data"]) {
                    return ;
                }
                @strongify(self);
                self.offset += 20;
                NSArray *array = [NSArray modelArrayWithClass:[HSForumListModel class] json:dict[@"data"]];
                [self renewForums:array withUsers:[NSArray modelArrayWithClass:[HSForumListModel class] json:dict[@"included"]]];
                [self.forums addObjectsFromArray:array];
            } failed:^(NSError *error) {
                
            }].racSignal;
        }];
    }
    return _loadmoreCommand;
}

- (void)renewForums:(NSArray<HSForumListModel *> *)forums withUsers:(NSArray<HSForumListModel *>*)users
{
    NSMutableArray<HSForumListModel *> *__users = @[].mutableCopy;
    [users enumerateObjectsUsingBlock:^(HSForumListModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (![obj.type isEqualToString:@"users"]) {
            *stop = YES;
        }
        [__users addObject:obj];
    }];
    [forums enumerateObjectsUsingBlock:^(HSForumListModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [__users enumerateObjectsUsingBlock:^(HSForumListModel * _Nonnull obj2, NSUInteger idx2, BOOL * _Nonnull stop2) {
            if ([obj.relationships.startUser.data.id isEqualToString:obj2.id]) {
                obj.attributes.username = obj2.attributes.username;
                obj.attributes.avatarUrl = obj2.attributes.avatarUrl;
                *stop2 = YES;
            }
        }];
    }];
}

@end
